package server.startup;

import server.Mapper;
import server.Valve;
import server.connector.http.HttpConnector;
import server.core.SimpleContext;
import server.core.SimpleContextMapper;
import server.core.SimpleLoader;
import server.core.SimpleWrapper;
import server.valves.ClientIPLoggerValve;
import server.valves.HeaderLoggerValve;

public class Bootstrap2 {
    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();

        SimpleWrapper wrapper1 = new SimpleWrapper();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");
        SimpleWrapper wrapper2 = new SimpleWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        SimpleContext context = new SimpleContext();
        context.addChild(wrapper1);
        context.addChild(wrapper2);

        Valve valve1 = new HeaderLoggerValve();
        Valve valve2 = new ClientIPLoggerValve();
        context.addValve(valve1);
        context.addValve(valve2);

        Mapper mapper = new SimpleContextMapper();
        mapper.setProtocol("http");
        context.addMapper(mapper);

        SimpleLoader loader = new SimpleLoader();
        context.setLoader(loader);

        context.addServletMapping("/Primitive", "Primitive");
        context.addServletMapping("/Modern", "Modern");

        connector.setContainer(context);

        try {
            connector.initialize();
            connector.start();
            // make the application wait until we press a key.
            System.in.read();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
